package com.jkkc.lib.base.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;

/**
 * @Description: TODO
 */
public class DateUtil {


    private static final String DATE_FORMAT_PATTERN_YMD = "yyyy-MM-dd";
    private static final String DATE_FORMAT_PATTERN_YMD_HM = "yyyy-MM-dd HH:mm";
    public static final String DATE_FORMAT_PATTERN_YMD_HM_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_PATTERN_Y_M_D_H_M_S = "yyyy_MM_dd_HH_mm_ss";
    public static final String DATE_FORMAT_PATTERN_YMDHMS = "yyyyMMddHHmmss";


    private final static long minute = 60 * 1000;// 1分钟
    private final static long hour = 60 * minute;// 1小时
    private final static long day = 24 * hour;// 1天
    private final static long month = 31 * day;// 月
    private final static long year = 12 * month;// 年


    /**
     * 时间戳转字符串
     *
     * @param timestamp     时间戳
     * @return 格式化的日期字符串
     */
    public static String long2Str(long timestamp) {
        return long2Str(timestamp, DATE_FORMAT_PATTERN_YMD_HM_SS);
    }

    private static String long2Str(long timestamp, String pattern) {
        return new SimpleDateFormat(pattern, Locale.CHINA).format(new Date(timestamp));
    }

    /**
     * 字符串转时间戳
     *
     * @param dateStr       日期字符串
     * @param isPreciseTime 是否包含时分
     * @return 时间戳
     */
    public static long str2Long(String dateStr, boolean isPreciseTime) {
        return str2Long(dateStr, getFormatPattern(isPreciseTime));
    }

    public static long str2Long(String dateStr, String pattern) {
        try {
            return Objects.requireNonNull(new SimpleDateFormat(pattern, Locale.CHINA).parse(dateStr)).getTime();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return 0;
    }

    private static String getFormatPattern(boolean showSpecificTime) {
        if (showSpecificTime) {
            return DATE_FORMAT_PATTERN_YMD_HM;
        } else {
            return DATE_FORMAT_PATTERN_YMD;
        }
    }



    public static String todayStr() {
        return dateFormat(new Date());
    }

    /**
     * @Description: 获取格式化后的现在的年月日yyyy-MM-dd
     */
    public static String getStrDate() {
        return dateFormat(new Date());
    }
    public static String dateFormat(Date date) {
        return dateFormat(date, "yyyy-MM-dd");
    }


    /**
     * @Description: 获取格式化后的现在的时分秒HH:mm:ss
     */
    public static String getStrTime() {
        return formatTime(new Date());
    }
    public static String formatTime(Date date) {
        return dateFormat(date, "HH:mm:ss");
    }


    /**
     * @Description: 获取格式化后的现在的年月日时分秒yyyy-MM-dd HH:mm:ss
     */
    public static String getStrDateTime() {
        return formatDateTime(new Date());
    }
    public static String formatDateTime(Date date) {
        return dateFormat(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String formatTimestamp(long timestamp, String pattern) {
        return new SimpleDateFormat(pattern, Locale.CHINA).format(timestamp);
    }
    public static String timestamp2StrDate(long timestamp) {
        return formatTimestamp(timestamp, "yyyy-MM-dd");
    }
    public static String timestamp2StrDateTime(long timestamp) {
        return formatTimestamp(timestamp, "yyyy-MM-dd HH:mm:ss");
    }
    public static long getFirstSecondsInDay(long timestamp) {
        return strToDate(formatTimestamp(timestamp, "yyyy-MM-dd")).getTime();
    }
    public static long getLastSecondsInDay(long timestamp) {
        return getFirstSecondsInDay(timestamp) + 1000 * 60 * 60 * 24 - 1;
    }

    /**
     * @Description: 获取自定义格式化时间
     */
    public static String dateFormat(Date date, String pattern) {
        return new SimpleDateFormat(pattern, Locale.CHINA).format(date);
    }

    /**
     * @Description: 字符串转换成自定义格式日期时间
     */
    public static Date strFormatToDate(String str, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.CHINA);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * @Description: 字符串转换成日期时间yyyy-MM-dd HH:mm:ss
     */
    public static Date strToDateTime(String str) {
        return strFormatToDate(str, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * @Description: 字符串转换成日期yyyy-MM-dd
     */
    public static Date strToDate(String str) {
        return strFormatToDate(str, "yyyy-MM-dd");
    }

    /**
     * @Description: 字符串转换成日期HH:mm:ss
     */
    public static Date strToTime(String str) {
        return strFormatToDate(str, "HH:mm:ss");
    }

    /**
     * @Description: 判断date是不是今天
     */
    public static boolean isToDay(Date date) {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int day = c.get(Calendar.DAY_OF_YEAR);
        c.setTime(date);
        return year == c.get(Calendar.YEAR) && day == c.get(Calendar.DAY_OF_YEAR);
    }

    /*
     * 将字符串时间转成几分钟前、几天前
     */
    public static String getTimeFormatText(String timeStr) {
        if (timeStr == null) {
            return null;
        }
        long diff = new Date().getTime() - str2Long(timeStr, true);
        long r = 0;
        if (diff > year) {
            r = (diff / year);
            return r + "年前";
        }
        if (diff > month) {
            r = (diff / month);
            return r + "个月前";
        }
        if (diff > day) {
            r = (diff / day);
            return r + "天前";
        }
        if (diff > hour) {
            r = (diff / hour);
            return r + "小时前";
        }
        if (diff > minute) {
            r = (diff / minute);
            return r + "分钟前";
        }
        return "刚刚";

    }

}
